package com.ustb.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ustb.springboot.common.utils.*;
import com.ustb.springboot.controller.dto.*;
import com.ustb.springboot.dao.*;
import com.ustb.springboot.entity.*;
import com.ustb.springboot.service.FormService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Array;
import java.util.*;


@Service("formService")
public class FormServiceImpl extends ServiceImpl<FormDao, FormEntity> implements FormService {

    //注入FormDao
    @Resource
    private FormDao formDao;
    @Resource
    private AnswerBlankDao answerBlankDao;
    @Resource
    private AnswerCheckboxDao answerCheckboxDao;
    @Resource
    private AnswerDatetimeDao answerDatetimeDao;
    @Resource
    private AnswerRadioDao answerRadioDao;
    @Resource
    private AnswerSlideDao answerSlideDao;
    @Resource
    private AnswerListboxDao answerListboxDao;
    @Resource
    private AnswerScoreDao answerScoreDao;
    //questionDao
    @Resource
    private QuestionBlankDao questionBlankDao;
    @Resource
    private QuestionRadioDao questionRadioDao;
    @Resource
    private QuestionCheckboxDao questionCheckboxDao;
    @Resource
    private QuestionListboxDao questionListboxDao;
    @Resource
    private QuestionSlideDao questionSlideDao;
    @Resource
    private QuestionDatetimeDao questionDatetimeDao;
    @Resource
    private OptionDao optionDao;
    @Resource
    private QuestionScoreDao questionScoreDao;

    /**
     * 传入表单ID(注意传入时进行校验表单是否存在 防止空指针错误)
     * 返回该表单的所有问题 按设计表单时的序号排序后返回
     */
    private List<Object> getQuestions(String formId) {
        List<Object> questions = new LinkedList<>();
        //type 0 blank  填空题封装到questions中
        questions.addAll(questionBlankDao.selectList(
                new QueryWrapper<QuestionBlankEntity>().eq("form_id", formId)
        ));

        //type 1 radio
        //所有单项选择题  封装option
        //循环插入到radioAnswerList
        questionRadioDao.selectList(
                new QueryWrapper<QuestionRadioEntity>().eq("form_id", formId)
        ).forEach(radio -> questions.add(new RadionAnswerDto()
                        .setId(radio.getId())
                        .setUserId(radio.getUserId())
                        .setFormId(radio.getFormId())
                        .setOrderId(radio.getOrderId())
                        .setMust(radio.getMust())
                        .setQuestionName(radio.getQuestionName())
                        .setQuestionType(radio.getQuestionType())
                        .setQuestionInfo(radio.getQuestionInfo())
                        .setOptions(
                                optionDao.selectList(
                                        new QueryWrapper<OptionEntity>().eq("question_id", radio.getId()))
                        )
                )
        );

        //type 2 checkbox question
        questionCheckboxDao.selectList(
                new QueryWrapper<QuestionCheckboxEntity>().eq("form_id", formId)
        ).forEach(checkbox -> questions.add(new CheckboxOptionDto()
                .setId(checkbox.getId())
                .setUserId(checkbox.getUserId())
                .setFormId(checkbox.getFormId())
                .setOrderId(checkbox.getOrderId())
                .setMust(checkbox.getMust())
                .setQuestionName(checkbox.getQuestionName())
                .setQuestionType(checkbox.getQuestionType())
                .setQuestionInfo(checkbox.getQuestionInfo())
                .setOptionMin(checkbox.getOptionMin())
                .setOptionMax(checkbox.getOptionMax())
                .setOptions(
                        optionDao.selectList(
                                new QueryWrapper<OptionEntity>().eq("question_id", checkbox.getId())
                        )
                ))
        );
        //type 3 listbox
        questionListboxDao.selectList(
                new QueryWrapper<QuestionListboxEntity>().eq("form_id", formId)
        ).forEach(listbox -> questions.add(
                new ListboxAnswerDto()
                        .setId(listbox.getId())
                        .setUserId(listbox.getUserId())
                        .setFormId(listbox.getFormId())
                        .setOrderId(listbox.getOrderId())
                        .setMust(listbox.getMust())
                        .setQuestionName(listbox.getQuestionName())
                        .setQuestionType(listbox.getQuestionType())
                        .setQuestionInfo(listbox.getQuestionInfo())
                        .setOptions(
                                optionDao.selectList(
                                        new QueryWrapper<OptionEntity>().eq("question_id", listbox.getId())
                                )
                        )
        ));

        //type 4 slide
        questions.addAll(questionSlideDao.selectList(
                new QueryWrapper<QuestionSlideEntity>().eq("form_id", formId)
        ));
        //type 5 datetime 时间日期
        questions.addAll(questionDatetimeDao.selectList(
                new QueryWrapper<QuestionDatetimeEntity>().eq("form_id", formId)
        ));
        //type 6 socre 评分题
        questions.addAll(questionScoreDao.selectList(
                new QueryWrapper<QuestionScoreEntity>().eq("form_id", formId)
        ));

        //根据序号排序
        questions.sort(new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                return getQuestionOrderId(o1) - getQuestionOrderId(o2);
            }
        });

        return questions;

    }

    /**
     * 根据传入的问题数组查找对应问题填写过的答案
     * <p>
     * 查询的是暂时保存的答案
     * is_submit==false
     */
    private List<Object> getAnswers(Integer userId, List<Object> questions) {
        List<Object> answers = new LinkedList<>();
        for (int i = 0; i < questions.size(); i++) {
            //根据问题类型去对应表格查询答案
            switch (getQuestionType(questions.get(i))) {
                case 0: {
                    //填空题
                    answers.add(answerBlankDao.selectOne(new QueryWrapper<AnswerBlankEntity>()
                            .eq("user_id", userId).eq("question_id", ((QuestionBlankEntity) questions.get(i)).getId()).eq("is_submit", false)));
                }
                break;
                case 1: {
                    //单选
                    answers.add(answerRadioDao.selectOne(new QueryWrapper<AnswerRadioEntity>()
                            .eq("user_id", userId).eq("question_id", ((RadionAnswerDto) questions.get(i)).getId()).eq("is_submit", false)));
                }
                break;
                case 2: {
                    //多选 CheckboxOptionDto
                    List<AnswerCheckboxEntity> answerCheckboxEntities = answerCheckboxDao.selectList(new QueryWrapper<AnswerCheckboxEntity>()
                            .eq("user_id", userId).eq("question_id", ((CheckboxOptionDto) questions.get(i)).getId()).eq("is_submit", false));
                    //没有填写过答案
                    if (answerCheckboxEntities == null) {
                        answers.add(null);
                        break;
                    }
                    ArrayList<String> optionId = new ArrayList<>();
                    answerCheckboxEntities.forEach(option -> optionId.add(option.getOptionId()));

                    answers.add(new CheckboxAnswerDto()
                            .setId(answerCheckboxEntities.get(0).getId())
                            .setUserId(answerCheckboxEntities.get(0).getUserId())
                            .setFormId(answerCheckboxEntities.get(0).getFormId())
                            .setQuestionId(answerCheckboxEntities.get(0).getQuestionId())
                            .setOptionId(optionId)
                            .setCreateTime(answerCheckboxEntities.get(0).getCreateTime())
                            .setIsSubmit(answerCheckboxEntities.get(0).getIsSubmit())
                    );
                }
                break;
                case 3: {
                    //下拉列表
                    answers.add(answerListboxDao.selectOne(new QueryWrapper<AnswerListboxEntity>()
                            .eq("user_id", userId).eq("question_id", ((ListboxAnswerDto) questions.get(i)).getId()).eq("is_submit", false)));

                }
                break;
                case 4: {
                    //滑块
                    answers.add(answerSlideDao.selectOne(new QueryWrapper<AnswerSlideEntity>()
                            .eq("user_id", userId).eq("question_id", ((QuestionSlideEntity) questions.get(i)).getId()).eq("is_submit", false)));

                }
                break;
                case 5: {
                    //日期时间
                    AnswerDatetimeEntity answerDatetime = answerDatetimeDao.selectOne(new QueryWrapper<AnswerDatetimeEntity>()
                            .eq("user_id", userId).eq("question_id", ((QuestionDatetimeEntity) questions.get(i)).getId()).eq("is_submit", false));

                    if (null == answerDatetime) {
                        answers.add(null);
                        break;
                    }
                    Date[] dates = {answerDatetime.getBeginTime(), answerDatetime.getEndTime()};
                    //日期时间
                    answers.add(new DateTimeAnswerDto().setId(answerDatetime.getId())
                            .setUserId(answerDatetime.getUserId())
                            .setFormId(answerDatetime.getFormId())
                            .setQuestionId(answerDatetime.getQuestionId())
                            .setCreateTime(answerDatetime.getCreateTime())
                            .setBeginAndEnd(dates));
                }
                break;
                case 6: {
                    //评分题
                    answers.add(answerScoreDao.selectOne(new QueryWrapper<AnswerScoreEntity>()
                            .eq("user_id", userId).eq("question_id", ((QuestionScoreEntity) questions.get(i)).getId()).eq("is_submit", false)));
                }
                break;
                default:
                    break;
            }

        }
        return answers;
    }

    //检查当前用户是否存在未提交的表单
    //false 存在未提交 true不存在 ->新表单
    private boolean checkIfSubmit(Integer userId, String formId) {
        //check in every answerTable

        //第一次 该问卷对应id与userId都没有记录
        //之前填过 但是未提交
        Map<String, Object> map = new HashMap<>();
        //存在未提交的答案 就说明
        map.put("user_id", userId);
        map.put("form_id", formId);
        map.put("is_submit", false);

        if (
                answerBlankDao.selectByMap(map).size() != 0
                        || answerRadioDao.selectByMap(map).size() != 0
                        || answerCheckboxDao.selectByMap(map).size() != 0
                        || answerListboxDao.selectByMap(map).size() != 0
                        || answerDatetimeDao.selectByMap(map).size() != 0
                        || answerSlideDao.selectByMap(map).size() != 0
                        || answerScoreDao.selectByMap(map).size() != 0
        )
            return false;
        else return true;
    }

    private Integer getQuestionType(Object o) {
        Integer type = null;
        if (o instanceof QuestionBlankEntity)
            type = ((QuestionBlankEntity) o).getQuestionType();
        else if (o instanceof RadionAnswerDto)
            type = ((RadionAnswerDto) o).getQuestionType();
        else if (o instanceof CheckboxOptionDto)
            type = ((CheckboxOptionDto) o).getQuestionType();
        else if (o instanceof ListboxAnswerDto)
            type = ((ListboxAnswerDto) o).getQuestionType();
        else if (o instanceof QuestionSlideEntity)
            type = ((QuestionSlideEntity) o).getQuestionType();
        else if (o instanceof QuestionDatetimeEntity)
            type = ((QuestionDatetimeEntity) o).getQuestionType();
        return type;
    }

    private Integer getQuestionOrderId(Object o) {

        Integer orderId = 0;
        if (o instanceof QuestionBlankEntity)
            orderId = ((QuestionBlankEntity) o).getOrderId();
        else if (o instanceof RadionAnswerDto)
            orderId = ((RadionAnswerDto) o).getOrderId();
        else if (o instanceof CheckboxOptionDto)
            orderId = ((CheckboxOptionDto) o).getOrderId();
        else if (o instanceof ListboxAnswerDto)
            orderId = ((ListboxAnswerDto) o).getOrderId();
        else if (o instanceof QuestionSlideEntity)
            orderId = ((QuestionSlideEntity) o).getOrderId();
        else if (o instanceof QuestionDatetimeEntity)
            orderId = ((QuestionDatetimeEntity) o).getOrderId();
        return orderId;

    }

    //获取表单中数据的一些函数

    /*************************************************************************************/

    //修改表单状态
    @Override
    public R editFormStatus(Map<String, Object> body) {
        //参数校验
        if (body.isEmpty() || !body.containsKey("formId") || !body.containsKey("formStatus"))
            return R.error(2000, "修改失败！");
        //获取参数
        String formId = body.get("formId").toString();
        Integer formStatus = Integer.valueOf(body.get("formStatus").toString());
        //数据库查询
        QueryWrapper<FormEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", formId);
        //更新对应表单的状态
        if (formDao.update(new FormEntity().setFormStatus(formStatus), queryWrapper) == 1)
            return R.ok(20000, "修改状态成功！");
        else return R.error(2000, "修改失败！");
    }

    /**
     * 具体实现
     * 判断当前展示表单是否第一次填写  是-->直接返回
     * 否-->调用showFormStorage
     **/
    @Override
    public R showForm(HttpServletRequest request, Map<String, Object> body) {
        //表单是否存在
        if (!body.containsKey("formId"))
            return R.error(2000, "参数错误！");
        String formId = body.get("formId").toString();

        //校验表单权限
        FormEntity formEntity = formDao.selectOne(new QueryWrapper<FormEntity>().eq("id", formId));
        if (null == formEntity) return R.error(2000, "表单不存在！");

        //是否允许匿名填写  不允许匿名且token非法 返回提示登录
        if ((!formEntity.getIsAnonymous()) && ((request.getHeader("token") == null) || (!JwtUtil.checkToken(request.getHeader("token")))))
            return R.error(2000, "请登录后填写！");


        //允许匿名填写 检测是否有token 有id为userId  否则为-1
        //允许匿名填写
        //用户id
        Integer userId = formEntity.getIsAnonymous() ? -1 : TokenUtil.getId(request);

        //userId!=-1 非匿名 答案 && userId在答案表 isSubmit为false
        if (userId != -1 && !checkIfSubmit(userId, formId))
            return this.showFormStorage(userId, formId);

        //调用带答案查询的返回
        //userId==-1 || checkIfSubmit
        //返回的数据  不需要查答案
        Map<String, Object> form = new HashMap<>();
        form.put("formInfo", formEntity);

        List<Object> questions = getQuestions(formId);
        form.put("questions", questions);
        form.put("userId", userId);
        form.put("type", "开始填写一份新的表单吧！");
        return R.ok(20000, "查询成功").put("form", form);
    }



    /**
     * 暂存的答案和问题
     */
    private R showFormStorage(Integer userId, String formId) {


        FormEntity formEntity = formDao.selectOne(new QueryWrapper<FormEntity>().eq("id", formId));
        if (formEntity == null) return R.error(2000, "表单不存在");
        Map<String, Object> form = new HashMap<>();
        form.put("formInfo", formEntity);
        //问题list questions
        List<Object> questions = getQuestions(formId);
        //答案 list answers
        List<Object> answers = getAnswers(userId, questions);
        //遍历questions

        form.put("questions", questions);
        form.put("answers", answers);
        form.put("userId", userId);
        form.put("type", "该表单还未填写完毕,请继续填写!");
        return R.ok(20000, "查询成功").put("form", form);
    }


    //ToDo
    @Override
    public R submitForm(HttpServletRequest request, Map<String, Object> body) {
        if (null == body || !body.containsKey("formId"))
            return R.error(2000, "参数传递有误");

        String formId = body.get("formId").toString();

        //校验表单权限
        FormEntity formEntity = formDao.selectOne(new QueryWrapper<FormEntity>().eq("id", formId));
        if (null == formEntity) return R.error(2000, "表单不存在！");

        if ((!formEntity.getIsAnonymous()) && ((request.getHeader("token") == null) || (!JwtUtil.checkToken(request.getHeader("token")))))
            return R.error(2000, "请登录后提交！");

        //允许匿名填写 检测是否有token 有id为userId  否则为-1
        //允许匿名填写
        //用户id
        Integer userId = formEntity.getIsAnonymous() ? -1 : TokenUtil.getId(request);

        //更新表单数量
        formDao.updateById(formEntity.setCounts(formEntity.getCounts() + 1));//更新

        //修改答案表
        //type 0
        answerBlankDao.selectList(
                new QueryWrapper<AnswerBlankEntity>().eq("form_id", formId).eq("user_id", userId).eq("is_submit", false)
        ).forEach(i -> answerBlankDao.updateById(i.setIsSubmit(true)));
        //type 1
        answerRadioDao.selectList(
                new QueryWrapper<AnswerRadioEntity>().eq("form_id", formId).eq("user_id", userId).eq("is_submit", false)
        ).forEach(i -> answerRadioDao.updateById(i.setIsSubmit(true)));
        //type 2
        answerCheckboxDao.selectList(
                new QueryWrapper<AnswerCheckboxEntity>().eq("form_id", formId).eq("user_id", userId).eq("is_submit", false)
        ).forEach(i -> answerCheckboxDao.updateById(i.setIsSubmit(true)));
        //type 3
        answerListboxDao.selectList(
                new QueryWrapper<AnswerListboxEntity>().eq("form_id", formId).eq("user_id", userId).eq("is_submit", false)
        ).forEach(i -> answerListboxDao.updateById(i.setIsSubmit(true)));
        //type 4
        answerSlideDao.selectList(
                new QueryWrapper<AnswerSlideEntity>().eq("form_id", formId).eq("user_id", userId).eq("is_submit", false)
        ).forEach(i -> answerSlideDao.updateById(i.setIsSubmit(true)));
        //type 5
        answerDatetimeDao.selectList(
                new QueryWrapper<AnswerDatetimeEntity>().eq("form_id", formId).eq("user_id", userId).eq("is_submit", false)
        ).forEach(i -> answerDatetimeDao.updateById(i.setIsSubmit(true)));
        //type 6
        answerScoreDao.selectList(
                new QueryWrapper<AnswerScoreEntity>().eq("form_id", formId).eq("user_id", userId).eq("is_submit", false)
        ).forEach(i -> answerScoreDao.updateById(i.setIsSubmit(true)));

        return R.ok(20000, "提交成功");
    }

    //ToDo
    @Override
    public R deleteFormById(Map<String, Object> body) {
        return null;
    }

    //ToDo
    @Override
    public R checkResult(Map<String, Object> body) {
        return null;
    }

    //创建表单
    @Override
    public R saveForm(HttpServletRequest request, Map<String, Object> body) {
        //null检测
        if (null == body || !body.containsKey("formId") || !body.containsKey("formName"))
            return R.error(2000, "创建表单失败！");
        //创建者id
        Integer userId = TokenUtil.getId(request);
        //表单id
        String formId = body.get("formId").toString();
        //表单名称
        String formName = body.get("formName").toString();
        //url  ToDo
        String formUrl = "http://localhost:89/#/others/user_fill/?formId=" + formId;
        //sql插入  设置各属性
        int insert = formDao.insert(
                new FormEntity()
                        .setId(formId)
                        .setUserId(userId)
                        .setFormName(formName)
                        .setCounts(0)
                        .setFormStatus(0)
                        .setFormUrl(formUrl)
                        .setCreateTime(new Date(System.currentTimeMillis()))
                        .setTimeLimit(false)
                        .setIpLimit(false)
                        .setIsAnonymous(true)
        );
        if (insert == 1)
            return R.ok(20000, "创建表单成功！");
        else
            return R.error(2000, "创建失败！");
    }


    //分页查询
    @Override
    public R queryFormList(HttpServletRequest request, Map<String, Object> body) {
        if (null == body)
            return R.error(2000, "请求体为空");
        if (!body.containsKey("pageNo") || !body.containsKey("pageSize"))
            return R.error(2000, "没有分页参数！");
        //用户id
        Integer userId = TokenUtil.getId(request);
        //创建查询
        QueryWrapper<FormEntity> queryWrapper = new QueryWrapper<>();
        //查询条件  0表名 1状态 2时间
        Integer selectBy = Integer.parseInt(body.get("selectBy").toString());
        //查询条件内容
        String selectContent = body.get("selectContent").toString();
        //
        if (body.containsKey("RecycleBin"))
            queryWrapper.eq("user_id", userId).eq("form_status", -1).orderByDesc("create_time");
            //按表名字
        else if (selectBy == 0)
            queryWrapper.eq("user_id", userId).like("form_name", selectContent).ge("form_status", 0).orderByDesc("create_time");
            //按表状态
        else if (selectBy == 1)
            queryWrapper.eq("user_id", userId).eq("form_status", Integer.parseInt(selectContent)).ge("form_status", 0).orderByDesc("create_time");


        //查询
        Page<FormEntity> formEntityPage = formDao.selectPage(
                new Page<>(Integer.parseInt(body.get("pageNo").toString()), Integer.parseInt(body.get("pageSize").toString()))
                , queryWrapper);
        Long counts = formDao.selectCount(queryWrapper);
        if (null == formEntityPage)
            return R.ok(2000, "暂无表单!");
        else return R.ok(20000, "查询成功!").put("forms", formEntityPage.getRecords()).put("counts", counts);
    }


    @Override
    //以List形式返回
    public List<FormEntity> querywithId(String Id){
        QueryWrapper<FormEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", Id);
        return formDao.selectList(queryWrapper);
    }

    @Override
    public Map<String, Object> summerizeForm(String FormId) {
        List<FormEntity> FormMessage = new ArrayList<>();
        FormMessage = querywithId(FormId);
        Map<String,Object> FormSummrizeMessage = new HashMap<>();
        FormSummrizeMessage.put("FormMessage",FormMessage);
        //TODO:信息未填充完成
        return FormSummrizeMessage;
    }

    @Override
    public R saveWork(HttpServletRequest request, Map<String, Object> body) {
        if (null == body || !body.containsKey("formId") || !body.containsKey("formName"))
            return R.error(2000, "保存业务表失败");
        //创建者id
        Integer userId = TokenUtil.getId(request);
        //url  ToDo
        String formUrl = "http://localhost:89/#/others/user_fill/?formId=" + body.get("formId").toString();
        //sql插入  设置各属性
        int insert = formDao.insert(
                new FormEntity()
                        .setId(body.get("id").toString())
                        .setUserId(userId)
                        .setFormName(body.get("formName").toString())
                        .setCounts(Integer.parseInt(body.get("counts").toString()))
                        .setFormInfo(body.get("formInfo").toString())
                        .setFormStatus(Integer.parseInt(body.get("formStatus").toString()))
                        .setFormUrl(formUrl)
                        .setCreateTime(new Date(System.currentTimeMillis()))
                        .setTimeLimit(false)
                        .setBeginTime(null)
                        .setEndTime(null)
                        .setFormPassword(null)
                        .setIpLimit(false)
                        .setIsAnonymous(true)
                        .setDepartmentId(null)
        );
        if (insert == 1)
            return R.ok(20000, "保存成功！");
        else
            return R.error(2000, "保存失败！");

    }

    @Override
    public List<FormEntity> queryWithdepartmentId(Map<String, Object> body) {
        QueryWrapper<FormEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("department_id"));
        return formDao.selectList(queryWrapper);
    }

    @Override
    //查询一个问题表单中所有的回答内容
    //输入：form_id
    //输出：表单中所有问题的答案
    public Map<String, Object> QueryFormResult(HttpServletRequest request, Map<String, Object> body) {
        Integer userId = TokenUtil.getId(request);
        String formId = body.get("form_id").toString();
        Map<String,Object> TotalAnswer = new HashMap<String , Object>();
        //查表
        QueryWrapper<FormEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",formId);
        List<FormEntity> queryWrapperList = formDao.selectList(queryWrapper);

        QueryWrapper<AnswerBlankEntity> answerBlankEntityQueryWrapper = new QueryWrapper<>();
        answerBlankEntityQueryWrapper.eq("form_id",formId);
        List<AnswerBlankEntity> answerBlankEntityList = answerBlankDao.selectList(answerBlankEntityQueryWrapper);

        QueryWrapper<AnswerCheckboxEntity> answerCheckboxQueryWrapper = new QueryWrapper<>();
        answerCheckboxQueryWrapper.eq("form_id",formId);
        List<AnswerCheckboxEntity> answerCheckboxEntityList = answerCheckboxDao.selectList(answerCheckboxQueryWrapper);

        QueryWrapper<AnswerDatetimeEntity>answerDatetimeEntityQueryWrapper = new QueryWrapper<>();
        answerDatetimeEntityQueryWrapper.eq("form_id",formId);
        List<AnswerDatetimeEntity> answerDatetimeEntityList = answerDatetimeDao.selectList(answerDatetimeEntityQueryWrapper);

        QueryWrapper<AnswerListboxEntity> answerListboxEntityQueryWrapper = new QueryWrapper<>();
        answerListboxEntityQueryWrapper.eq("form_id",formId);
        List<AnswerListboxEntity> answerListboxEntityList = answerListboxDao.selectList(answerListboxEntityQueryWrapper);

        QueryWrapper<AnswerRadioEntity> answerRadioEntityQueryWrapper = new QueryWrapper<>();
        answerRadioEntityQueryWrapper.eq("form_id",formId);
        List<AnswerRadioEntity> answerRadioEntityList = answerRadioDao.selectList(answerRadioEntityQueryWrapper);

        QueryWrapper<AnswerSlideEntity> answerSlideEntityQueryWrapper = new QueryWrapper<>();
        answerSlideEntityQueryWrapper.eq("form_id",formId);
        List<AnswerSlideEntity> answerSlideEntityList = answerSlideDao.selectList(answerSlideEntityQueryWrapper);

//        加入forminfo


        if (queryWrapperList != null)
            TotalAnswer.put("formInfo",queryWrapperList);
        if ( answerRadioEntityList != null )
            TotalAnswer.put("AnswerRadioEntityList",answerRadioEntityList);
        if ( answerCheckboxEntityList != null )
            TotalAnswer.put("AnswerCheckboxEntityList",answerCheckboxEntityList);
        if ( answerBlankEntityList != null)
            TotalAnswer.put("AnswerBlankEntityList",answerBlankEntityList);
        if ( answerDatetimeEntityList != null)
            TotalAnswer.put("AnswerDatetimeEntityList",answerDatetimeEntityList);
        if (answerListboxEntityList != null)
            TotalAnswer.put("AnswerListboxEntityList",answerListboxEntityList);
        if (answerSlideEntityList != null)
            TotalAnswer.put("AnswerSlideEntityList",answerSlideEntityList);
        return TotalAnswer;

//        if (null == TotalAnswer)
//            return R.ok(2000, "暂无信息!");
//        else
//            return R.ok(20000, "查询成功!").put("form", TotalAnswer);
    }
}